
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	
	self.Ignited = Sound("ambient.whoosh_large_incoming4")
	
	local burn = Sound("General.BurningObject")
	self.BurnSound = CreateSound(self, burn)
	
	local phys = self:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
		phys:SetMass(100)
	end
	
end

function ENT:Explode()
	if not self.Asploded then
		self.Asploded = true
		
		self.BurnSound:Stop()
		
		self.Pwner.CurDyn = nil
		
		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos())
		util.Effect("molotov_explosion", effectdata)

		self:ExplosionDamage(self.Pwner,self:GetPos(),70,290)
		self:EmitSound(ChooseRandom(GameSounds.Debris),120,math.random(95,105))
	
		self:Remove()
	end
end

function ENT:Use(ply,caller)
	if not self.DetTimer then
		self.DetTimer = CurTime() + math.Rand(7.8,9.8)
		self:EmitSound(self.Ignited,100,math.random(110,130))
		self.BurnSound:Play()
		self.Fuse = true
		self:SetOwner(ply)
	end	
end

function ENT:SetPwner(ply)
	self.Pwner = ply
end

function ENT:Think()
	if self.Fuse then
		self.Fuse = nil
		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos() + self:GetUp() * -4)
		effectdata:SetAngle(self:GetUp():Angle())
		util.Effect("dynamite_fuse", effectdata)
	end
	if self.DetTimer then
		if self.DetTimer < CurTime() then
			self.Exploded = true
		end
	end
	if self.Exploded then
		self:Explode() 
	end
end 
 
ENT.DamageCounter = 0
function ENT:OnTakeDamage( dmginfo )	
	self.DamageCounter = self.DamageCounter + 1
	if self.DamageCounter > 5 then
		self.Exploded = true
	end
end

function ENT:PhysicsCollide( data, physobj )
	if (data.Speed > 40 && data.DeltaTime > 0.2 ) then
		self:EmitSound(ChooseRandom(GameSounds.PlasticHit),80,math.random(90,110))
	end
end
